import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:重建二叉树
 * User: wangxin
 * Date: 2024-11-21
 * Time: 21:17
 */
public class test2 {
    public TreeNode reConstructBinaryTree (int[] preOrder, int[] vinOrder) {
        // write code here
        int n = preOrder.length;
        int m = vinOrder.length;
        if(m==0||n==0){
            return null;
        }
        TreeNode root = new TreeNode(preOrder[0]);
        for(int i = 0;i < m;i++){
            if(root.val == vinOrder[i]){
                root.left = reConstructBinaryTree(Arrays.copyOfRange(preOrder, 1, i + 1), Arrays.copyOfRange(vinOrder, 0, i));
                root.right = reConstructBinaryTree(Arrays.copyOfRange(preOrder,i+1,n),Arrays.copyOfRange(vinOrder,i+1,m));
                break;
            }

        }
        return root;
    }
}
